package com.hmall.item;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.ItemDoc;
import com.hmall.item.service.impl.ItemService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@SpringBootTest
public class testHmall {
    @Autowired
    private RestHighLevelClient client;

//    @Autowired
//    private ItemClient itemClient;
    @Autowired
    private ItemService itemService;

    @Test
    void hmallWhile() throws IOException {
        int i = 1;
        while (true) {
    /*PageDTO<Item> page = itemClient.getList(i,4500);
      List<Item> list = page.getList();*/
            BulkRequest request = new BulkRequest();

            Page<Item> pageInfo = new Page<>(i, 100);

            LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
            Page<Item> itemPage = itemService.page(pageInfo, wrapper);

            List<Item> list = itemPage.getRecords();
            if (CollectionUtils.isEmpty(list)) {
                break;
            }
            for (Item item : list) {
                ItemDoc itemDoc = new ItemDoc();
                itemDoc.setId(item.getId());
                itemDoc.setName(item.getName());
                itemDoc.setPrice(item.getPrice());
                itemDoc.setBrand(item.getBrand());
                itemDoc.setIsAD(item.getIsAD());
                itemDoc.setImage(item.getImage());
                itemDoc.setComment_count(item.getCommentCount());
                itemDoc.setSold(item.getSold());
                itemDoc.setCategory(item.getCategory());
                List<String> s = new ArrayList<>();
                s.add(item.getBrand());
                s.add(item.getCategory());
                itemDoc.setSuggestion(s);
                request.add(new IndexRequest("hmall")
                        .id(itemDoc.getId().toString())
                        .source(JSON.toJSONString(itemDoc), XContentType.JSON));
            }
            client.bulk(request, RequestOptions.DEFAULT);
            i++;
        }
    }

    @Test
    void getList() throws IOException {


        BulkRequest request = new BulkRequest();
        for (int i = 1; i <= 90; i++) {
      /*PageDTO<Item> page = itemClient.getList(i,4500);
      List<Item> list = page.getList();*/

            Page<Item> pageInfo = new Page<>(i, 1000);


            LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
            Page<Item> itemPage = itemService.page(pageInfo, wrapper);

            List<Item> list = itemPage.getRecords();
            for (Item item : list) {


                ItemDoc itemDoc = new ItemDoc();
                itemDoc.setId(item.getId());

                itemDoc.setName(item.getName());
                itemDoc.setPrice(item.getPrice());
                itemDoc.setBrand(item.getBrand());
                itemDoc.setIsAD(item.getIsAD());
                itemDoc.setImage(item.getImage());
                itemDoc.setComment_count(item.getCommentCount());
                itemDoc.setSold(item.getSold());
                itemDoc.setCategory(item.getCategory());
                List<String> s = new ArrayList<>();
                s.add(item.getBrand());
                s.add(item.getCategory());
                itemDoc.setSuggestion(s);

                request.add(new IndexRequest("hmall")
                        .id(itemDoc.getId().toString())
                        .source(JSON.toJSONString(itemDoc), XContentType.JSON));
            }
            client.bulk(request, RequestOptions.DEFAULT);
            System.out.println(i);
        }
    }

}
